package controller;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.*;

/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private LoginDao ldao;
	private Listener listener;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        ldao = new LoginDao();
        listener = new Listener();
        listener.start();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("Login get");		
		request.getRequestDispatcher("WEB-INF/LoginPage.jsp").forward(request, response);		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String interval = request.getParameter("interval");
			if(interval == null){
				System.out.println("Login post");
				String user = request.getParameter("user");
				System.out.println("user: " + user);
				String pass = request.getParameter("pass");
				System.out.println("pass: " + pass);
				if(ldao.authorize(user, pass)) {
					System.out.println("success");
					request.getSession().setAttribute("user", user);
					
					//Getting and Setting type
					ResultSet rs = ldao.selectUserbyName(user);
					String type = null;
					try {
						while(rs.next()) {					
							type = rs.getString("type");					
						}
					} catch (SQLException se) {
						se.printStackTrace();
					}
					System.out.println(type);
					request.getSession().setAttribute("type", type);
					
					if((request.getRemoteAddr().startsWith("0:0:0")) || (request.getRemoteAddr().startsWith("127.0.0")) || (request.getRemoteAddr().startsWith("10.0.")) || (request.getRemoteAddr().startsWith("192.168."))){
						request.getSession().setAttribute("lanuser", true);
						System.out.println("Lanuser detected");
						
					} else{
						System.out.println("Van buitenaf ingelogd!");
						request.getSession().setAttribute("lanuser", false);
					}			
					RequestDispatcher r = request.getRequestDispatcher("WEB-INF/home.jsp");
					r.forward(request, response);			
					
				}else{
					//Login fails then redirect to LoginPage.jsp
					System.out.println("no success");			
					request.setAttribute("fail", true);						
					RequestDispatcher r = request.getRequestDispatcher("WEB-INF/LoginPage.jsp");
					r.forward(request, response);
					
				}
		}else{
			System.out.println("changing interval...");
			listener.changeInterval(interval);
			System.out.println("changed interval");
			RequestDispatcher r = request.getRequestDispatcher("WEB-INF/home.jsp");
			r.forward(request, response);
		}
	}
}
